package com.ujinsung.norebang.yahoo.newsfeeds
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	public class NewsFeedProxy extends EventDispatcher
	{
		public static var topStoryPath:String = "http://rss.news.yahoo.com/rss/topstories";
		public static var INIT:String = "newsfeed_init";
		
		private var _xml:XML;
		private var _items:Array;
		private var _titles:Array;
		private var _images:Array;
		private var mediaNs:Namespace;
		
		public function NewsFeedProxy()
		{
			var loader:URLLoader = new URLLoader();
			var request:URLRequest = new URLRequest( topStoryPath );
			
			loader.addEventListener( Event.COMPLETE, init );
			loader.load( request );
		}
		
		public function init( event: Event):void {
			_xml =  XML(event.target.data);
			_titles = new Array();
			_images = new Array();
			mediaNs = new Namespace("http://search.yahoo.com/mrss/");
			for (var i:uint = 0; i < XMLList(_xml.channel.item).length(); i++) {
				var type:String = _xml.channel.item[i].mediaNs::content.@type.toString();
				if ( type.indexOf("image") > -1 ) {
					_titles.push ( _xml.channel.item[i].title );
					_images.push ( _xml.channel.item.mediaNs::content );
				}
			}
			
			this.dispatchEvent( new Event ( INIT ) );
		}
		
		public function get titles():Array {
			return _titles;
		}
		
		public function get images():Array {
			return _images;
		}

	}
}